package com.xydtech.ecard.ecard.service.impl;

import com.xydtech.common.utils.AESEncryptionUtils;
import com.xydtech.common.utils.StringUtils;
import com.xydtech.ecard.ecard.domain.EcardApiEnterpriseLocation;
import com.xydtech.ecard.ecard.domain.EcardApiLocationHandover;
import com.xydtech.ecard.ecard.mapper.EcardApiEnterpriseLocationMapper;
import com.xydtech.ecard.ecard.mapper.EcardApiLocationHandoverMapper;
import com.xydtech.ecard.ecard.mapper.EcardApiMerchantTaskMapper;
import com.xydtech.ecard.ecard.service.IEcardApiEnterpriseLocationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 电子名片企业场所信息Service业务层处理
 *
 * @author yuzhiguo
 * @date 2024-07-26 下午5:09
 */
@Service
public class EcardApiEnterpriseLocationServiceImpl implements IEcardApiEnterpriseLocationService {

    @Autowired
    private EcardApiEnterpriseLocationMapper ecardEnterpriseLocationMapper;
    @Autowired
    private EcardApiMerchantTaskMapper ecardApiMerchantTaskMapper;
    @Autowired
    private EcardApiLocationHandoverMapper ecardApiLocationHandoverMapper;

    /**
     * 电子名片-保存场所信息
     * @param ecardApiEnterpriseLocation
     * @return
     */
    @Override
    public int addLocationInfo(EcardApiEnterpriseLocation ecardApiEnterpriseLocation, String salt) {
        try {
            //数据加密（手机号、身份证号）
            if (!StringUtils.objIsNullOrEmpty(ecardApiEnterpriseLocation.getLocationPhone())) {
                ecardApiEnterpriseLocation.setLocationPhone(AESEncryptionUtils.encrypt(ecardApiEnterpriseLocation.getLocationPhone(), salt));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return ecardEnterpriseLocationMapper.addLocationInfo(ecardApiEnterpriseLocation);
    }

    /**
     * 电子名片-查询场所信息详情接口
     * @param id
     * @return
     */
    @Override
    public EcardApiEnterpriseLocation selectEcardEnterpriseLocationById(Long id, String salt) {
        EcardApiEnterpriseLocation enterpriseLocation = ecardEnterpriseLocationMapper.selectEcardEnterpriseLocationById(id);
        if (enterpriseLocation != null) {
            try {
                //数据解密（手机号、身份证号）
                if (!StringUtils.objIsNullOrEmpty(enterpriseLocation.getLocationPhone())) {
                    enterpriseLocation.setLocationPhone(AESEncryptionUtils.decrypt(enterpriseLocation.getLocationPhone(), salt));
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        return enterpriseLocation;
    }

    /**
     * 根据ID修改场所信息状态
     * @param id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int changeLocationStatus(Long id) {
        return ecardEnterpriseLocationMapper.changeLocationStatus(id);
    }

    /**
     * 条件查询场所信息列表接口
     * @param ecardApiEnterpriseLocation
     * @return
     */
    @Override
    public List<EcardApiEnterpriseLocation> getLocationInfoList(EcardApiEnterpriseLocation ecardApiEnterpriseLocation, String salt) {
        try {
            //数据加密（手机号、身份证号）
            if (!StringUtils.objIsNullOrEmpty(ecardApiEnterpriseLocation.getLocationPhone())) {
                ecardApiEnterpriseLocation.setLocationPhone(AESEncryptionUtils.encrypt(ecardApiEnterpriseLocation.getLocationPhone(), salt));
            }
            List<EcardApiEnterpriseLocation> enterpriseLocationList = ecardEnterpriseLocationMapper.getLocationInfoList(ecardApiEnterpriseLocation);
            if (enterpriseLocationList.size() > 0) {
                for (EcardApiEnterpriseLocation enterpriseLocation : enterpriseLocationList) {
                    //数据解密（手机号、身份证号）
                    if (!StringUtils.objIsNullOrEmpty(enterpriseLocation.getLocationPhone())) {
                        enterpriseLocation.setLocationPhone(AESEncryptionUtils.decrypt(enterpriseLocation.getLocationPhone(), salt));
                    }
                }
            }
            return enterpriseLocationList;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 根据场所名称查询场所信息列表
     * @param locationName
     * @param deptId
     * @return
     */
    @Override
    public List<EcardApiEnterpriseLocation> selectEnterpriseLocationByLocationName(String locationName, Long deptId, String salt) {
        List<EcardApiEnterpriseLocation> enterpriseLocationList = ecardEnterpriseLocationMapper.selectEnterpriseLocationByLocationName(locationName, deptId);
        if (enterpriseLocationList.size() > 0) {
            for (EcardApiEnterpriseLocation enterpriseLocation : enterpriseLocationList) {
                try {
                    //数据解密（手机号、身份证号）
                    if (!StringUtils.objIsNullOrEmpty(enterpriseLocation.getLocationPhone())) {
                        enterpriseLocation.setLocationPhone(AESEncryptionUtils.decrypt(enterpriseLocation.getLocationPhone(), salt));
                    }
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        }
        return enterpriseLocationList;
    }

    /**
     * 场所信息转交
     * @param ecardLocationHandover
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int addLocationTransferInfo(EcardApiLocationHandover ecardLocationHandover) {
        //保存转交记录
        ecardApiLocationHandoverMapper.insertEcardLocationHandover(ecardLocationHandover);
        //更新场所信息的管户人id和机构id
        return ecardEnterpriseLocationMapper.updateLocationUserId(ecardLocationHandover.getLocationId(), ecardLocationHandover.getRecipientId(), ecardLocationHandover.getRecipientDeptId());
    }

    /**
     * 查询场所信息地图坐标列表接口
     * @param creatorId
     * @param deptId
     * @return
     */
    @Override
    public List<EcardApiEnterpriseLocation> getLocationCoordinateList(Long creatorId, Long deptId) {
        return ecardEnterpriseLocationMapper.getLocationCoordinateList(creatorId, deptId);
    }

    /**
     * 根据法人机构ID查询机构下所有场所信息坐标信息列表
     * @param deptId
     * @return
     */
    @Override
    public List<EcardApiEnterpriseLocation> getAllLocationCoordinateList(Long creatorId, Long deptId) {
        return ecardEnterpriseLocationMapper.getAllLocationCoordinateList(creatorId, deptId);
    }
}
